package cn.wp.common.core.web;

import cn.wp.common.core.Wp;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static cn.wp.common.core.WpVar.SUPER_TOKEN_VALUE;

@Configuration
@EnableSwagger2
@EnableSwagger2Doc
@Profile({"dev","test"})// 设置 dev test 环境开启 prod 环境就关闭了
@ConditionalOnExpression("${swagger.enable:true}")
public class WpSwagger2Config {

    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenParam = new ParameterBuilder();
        tokenParam.name(Wp.self().getAuthenticationProperties().getTokenName()).description("登录校验").defaultValue(SUPER_TOKEN_VALUE)
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        List<Parameter> params = new ArrayList<>();
        params.add(tokenParam.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(Wp.GlobalPackage))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(params);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("Wp-Framwork接口文档")
//                .termsOfServiceUrl("http:/xxx/xxx")
//                .contact("wealthypower")
//                .version("1.0")
                .build();
    }
}
